package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.world.WorldEvent;
import com.motu.monstercity.server.game.commondata.world.WorldMonsterLevel;
import com.motu.monstercity.server.game.commondata.world.WorldRadar;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class WorldBaseData {
    private static Logger logger = LoggerFactory.getLogger(WorldBaseData.class);

    private static ConcurrentHashMap<Integer, WorldRadar> worldRadarMap = new ConcurrentHashMap<>(); // 大世界雷达等级配置表
    private static ConcurrentHashMap<Integer, WorldEvent> worldEventMap = new ConcurrentHashMap<>(); // 大世界事件配置表
    private static ConcurrentHashMap<Integer, WorldMonsterLevel> worldMonsterLevelMap = new ConcurrentHashMap<>(); // 大世界怪物等级配置表
    private static int maxMonsterLevel = 0;// 怪兽的最大等级
    private static int worldEventOfferFirstId = 0;//  雷达提升等级时，解锁悬赏事件的第一个事件的id

    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initWorldRadar());
        futures.add(initWorldEvent());
        futures.add(initWorldMonsterLevel());
    }

    /**
     * 初始化大世界雷达等级配置表
     */
    public static Future<Void> initWorldRadar() {
        String table = "world_radar";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, WorldRadar> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        WorldRadar entity = new WorldRadar(result.getJsonObject(i));
                        tmp.put(entity.getLevel(), entity);
                    }
                    worldRadarMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static WorldRadar getWorldRadar(int level) {
        WorldRadar result = worldRadarMap.get(level);
        if (result == null) {
            logger.error("getWorldRadar error key = " + level);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, WorldRadar> getWorldRadarMap() { return worldRadarMap; }

    /**
     * 初始化大世界事件配置表
     */
    public static Future<Void> initWorldEvent() {
        String table = "world_event";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, WorldEvent> tmp = new ConcurrentHashMap<>();
                    int firstId = 0;// 悬赏事件的id
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        WorldEvent entity = new WorldEvent(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);

                        if (entity.isOfferEvent() && (firstId == 0 || entity.getId() < firstId)) {
                            firstId = entity.getId();
                        }
                    }
                    worldEventMap = tmp;
                    worldEventOfferFirstId = firstId;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    // 根据groupid 和quality 确定事件列表
    public static List<WorldEvent> getWoldEventByGroupIdAndQuality(int groupId, int quality) {
        List<WorldEvent> result = new ArrayList<>();
        for (WorldEvent worldEvent : worldEventMap.values()) {
            if (worldEvent.getEventGroup() == groupId && worldEvent.getQuality() == quality) {
                result.add(worldEvent);
            }
        }
        return result;
    }

    // 根据groupid  确定事件列表
    public static List<WorldEvent> getWoldEventByGroupId(int groupId) {
        List<WorldEvent> result = new ArrayList<>();
        for (WorldEvent worldEvent : worldEventMap.values()) {
            if (worldEvent.getEventGroup() == groupId) {
                result.add(worldEvent);
            }
        }
        return result;
    }

    public static WorldEvent getWorldEvent(int id) {
        WorldEvent result = worldEventMap.get(id);
        if (result == null) {
            logger.error("getWorldEvent error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, WorldEvent> getWorldEventMap() { return worldEventMap; }

    /**
     * 初始化大世界怪物等级配置表
     */
    public static Future<Void> initWorldMonsterLevel() {
        String table = "world_monster_level";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, WorldMonsterLevel> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        WorldMonsterLevel entity = new WorldMonsterLevel(result.getJsonObject(i));
                        tmp.put(entity.getLevel(), entity);
                        if (entity.getLevel() > maxMonsterLevel) {
                            maxMonsterLevel = entity.getLevel();
                        }
                    }
                    worldMonsterLevelMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static int getMaxMonsterLevel() {
        return maxMonsterLevel;
    }

    // 悬赏事件的初始事件id
    public static int getWorldEventOfferFirstId() {
        return worldEventOfferFirstId;
    }

    public static WorldMonsterLevel getWorldMonsterLevel(int id) {
        WorldMonsterLevel result = worldMonsterLevelMap.get(id);
        if (result == null) {
            logger.error("getWorldMonsterLevel error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, WorldMonsterLevel> getWorldMonsterLevelMap() { return worldMonsterLevelMap; }
}
